<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'material:main:pmMainmaterial:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.createtime }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMaterialMainPmMainmaterialList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmMainmaterialDelete,
    pmMainmaterialListData,
  } from '/@/api/material/main/pmMainmaterial';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('material.main.pmMainmaterial');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('选取主材料管理'),
  };

  const pcode = ref<string>(router.currentRoute.value.query.pcode as string);

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('创建时间'),
        field: 'createtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('项目名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('项目编码'),
        field: 'pcode',
        component: 'Input',
      },
      {
        label: t('sno'),
        field: 'sno',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('fdno'),
        field: 'fdno',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('材料编码'),
        field: 'mcode',
        component: 'Input',
      },
      {
        label: t('材料名称'),
        field: 'mname',
        component: 'Input',
      },
      {
        label: t('规格型号'),
        field: 'mstd',
        component: 'Input',
      },
      {
        label: t('数量'),
        field: 'num',
        component: 'Input',
      },
      {
        label: t('单位'),
        field: 'unit',
        component: 'Input',
      },
      {
        label: t('istart'),
        field: 'istart',
        component: 'Input',
        ifShow: false,
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 140,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('项目名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('项目编码'),
      dataIndex: 'pcode',
      key: 'a.pcode',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('sno'),
      dataIndex: 'sno',
      key: 'a.sno',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('fdno'),
      dataIndex: 'fdno',
      key: 'a.fdno',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('材料编码'),
      dataIndex: 'mcode',
      key: 'a.mcode',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('材料名称'),
      dataIndex: 'mname',
      key: 'a.mname',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'mstd',
      key: 'a.mstd',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'num',
      key: 'a.num',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('单位'),
      dataIndex: 'unit',
      key: 'a.unit',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('istart'),
      dataIndex: 'istart',
      key: 'a.istart',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑选取主材料'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'material:main:pmMainmaterial:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除选取主材料'),
        popConfirm: {
          title: t('是否确认删除选取主材料'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'material:main:pmMainmaterial:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmMainmaterialListData,
    beforeFetch: (params) => {
      params.pcode = pcode.value || '';
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmMainmaterialDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
